<?php
/*
 * $Id$
 *
 */

class Cellphonezone_Trackingnumber_TrackingNumberController extends Mage_Adminhtml_Controller_Action
{	
    /**
     * Form to import tracking files
     *
     */
    public function IndexAction()
    {
		$this->loadLayout();
		$this->renderLayout();   	
    }
    
    /**
     * Process tracking file
     *
     */
    public function SubmitTrackingFileAction()
    {
    	//init
    	$ct_id = $this->getRequest()->getPost('ct_id');
    	$template = mage::getModel('Orderpreparation/CarrierTemplate')->load($ct_id);
		    	
    	try 
    	{
	    	//save text file
		    $uploader = new Varien_File_Uploader('file');
		    $uploader->setAllowedExtensions(array('txt','csv'));
		    $path = Mage::app()->getConfig()->getTempVarDir().'/import/';
		    $uploader->save($path);
		    
	   		//check
		    if ($uploadFile = $uploader->getUploadedFileName()) 
		    {
		    	//import file
		    	$filePath = $path.$uploadFile;
		    	$t_lines = file($filePath);
		    	$result = $template->importTrackingFile($t_lines);
		    		    	
		    	//display result
				Mage::getSingleton('adminhtml/session')->addSuccess($result);
				$this->_redirect('*/*/'); 
		    }
		    else 
		    	throw new Exception($this->__('Unable to upload file'));

    	}
    	catch (Exception $ex)
    	{
	    	Mage::getSingleton('adminhtml/session')->addError($ex->getMessage());
	    	$this->_redirect('*/*/');   
    	}
 
    }
}